package com.woniuxy.carrental.controller;

import com.woniuxy.carrental.entity.*;
import com.woniuxy.carrental.service.CarOrderService;
import com.woniuxy.carrental.service.RenrCarInfoService;
import com.woniuxy.carrental.service.StoreService;
import com.woniuxy.carrental.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class CarOrderController {
    @Resource
    CarOrderService carOrderService;
    @Resource
    UserService userService;
    @Resource
    StoreService storeService;
    @Resource
    RenrCarInfoService renrCarInfoService;

    @GetMapping("/carOrder")
    public ResponseResult<PageBean<CarOrder>> getCarOrders(CarOrder carOrder, Integer pageIndex, Integer pageSize){
        System.out.println(carOrder); //查询条件
        carOrder.setOrder_status("待还");
        List<CarOrder> carOrders = carOrderService.findAllCarOrder(carOrder, pageIndex, pageSize);
        PageBean<CarOrder> pageBean = new PageBean<>();
        pageBean.setBeanList(carOrders);
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalRecord(carOrderService.findCountCarOrder(carOrder.getOrder_status()));
        return new ResponseResult<PageBean<CarOrder>>(pageBean,200,"OK");
    }

    @GetMapping("/pickCarOrder")
    public ResponseResult<PageBean<CarOrder>> getPickCarOrders(CarOrder carOrder, Integer pageIndex, Integer pageSize){
        System.out.println(carOrder); //查询条件
        carOrder.setOrder_status("待取");
        List<CarOrder> carOrders = carOrderService.findAllCarOrder(carOrder, pageIndex, pageSize);
        PageBean<CarOrder> pageBean = new PageBean<>();
        pageBean.setBeanList(carOrders);
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalRecord(carOrderService.findCountCarOrder(carOrder.getOrder_status()));
        return new ResponseResult<PageBean<CarOrder>>(pageBean,200,"OK");
    }

    @DeleteMapping("/carOrder/{order_id}")
    public ResponseResult removeCarOrder(@PathVariable("order_id") String order_id){
        System.out.println(order_id);
        carOrderService.removeCarOrder(order_id);
        return new ResponseResult(null,200,"OK");
    }

    @PostMapping("/carOrder")
    public ResponseResult addCarOrder(@RequestBody TestCarOrder testCarOrder){
        CarOrder carOrder = new CarOrder();
        carOrder.setOrder_id(CommonUtil.uuid());
        carOrder.setPick_time(testCarOrder.getPick_time());
        carOrder.setLease_time(testCarOrder.getLease_time());
        carOrder.setPay_money(testCarOrder.getPay_money());
        carOrder.setOrder_status(testCarOrder.getOrder_status());
        User user = userService.getUserByPhone(testCarOrder.getPhone());
        if(user == null){
            return new ResponseResult(null,400,"error");
        }
        if(!user.getIdcard().equals(testCarOrder.getIdcard())){
            return new ResponseResult(null,400,"error");
        }
        Store store = storeService.findStoreBySname(testCarOrder.getStore());
        RentCarInfo rentCarInfo = renrCarInfoService.getRentCarInfoByCarnumber(testCarOrder.getCar_number());
        carOrder.setUser(user);
        carOrder.setRentCarInfo(rentCarInfo);
        carOrder.setStore(store);
        carOrderService.addCarOrder(carOrder);
        return new ResponseResult(null,200,"OK");
    }

}
